package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.API.JuheDemo;


/**
 * Servlet implementation class APIServlet
 */
@WebServlet("/APIServlet")
public class APIServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String APPKEY ="cb1f1b57bd7736ba60280044c6b92480";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public APIServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("getCityWethor".equals(op)) {
			try {
				doFind(request,response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
       
	}

	private void doFind(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String result =null;
		String city = request.getParameter("input");
		System.out.println(city);
       
		 String url ="http://op.juhe.cn/onebox/weather/query";//请求接口地址
	        Map params = new HashMap();//请求参数
	            params.put("cityname",city);//要查询的城市，如：温州、上海、北京
	            params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
	            params.put("dtype","");//返回数据的格式,xml或json，默认json
	     
	     result = JuheDemo.net(url, params, "GET");
	          
            response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
